Hey there.
In this lecture, we're going to be discussing and learning a feature in the Lulu programming language
called string interpolation.
Now the word interpolation means to insert something of a different nature into something else, which
is exactly what string interpolation allows us to do.
If we ever need to insert or concatenate something to a string, such as a table or the name of an instance
or any other property of an instance, we would usually have to get that value stored in that property,
and try to concatenate it to the table using two dots, or by using the string dot format function.
However, there is a much easier and more efficient way of inserting these things directly into our
strings, and that's by using string interpolation.
So to demonstrate string interpolation, I'm going to create a new server script and server script service.
And inside of this script what we're going to do is we're going to get the player service.
And we're going to print a message into the console when a new player joins our game.
So we could do players dot player added connect, get that player and then we would print out in the
console.
This player has joined the game.
Now.
Usually how we would do it is we would do player dot name and then concatenate it with this string.
Or another way we could do it is by using the string dot format function and then placing our string
in there.
And then we would add a directive into the string.
And then we would pass the string that we would like to replace that directive, which would be the
player's name.
See this is just like really long to type out and it doesn't look that clean.
However, using string interpolation, we can insert the player's name directly into our string, and
it's very easy to do so.
To use string interpolation, we can create a new string by using the Backtick key, which is located
directly above your tab key on the left hand side of your keyboard.
That's right, we can actually create a string in four different ways using either double quotes, single
quotes, double brackets, or using a Backtick.
This backtick signifies that this string can be interpolated.
So to insert things directly into the string, we have to use a pair of curly brackets, and between
these brackets is where our item is going to go inside of our string.
So after these curly brackets, I can say something like this player has joined the game, and inside
of these curly brackets I can go ahead and insert my player dot name property.
And just like that, we have interpolated the string by inserting this property directly into the string
itself.
So if I were to go and playtest my game and my player were to join, as you can see, it prints out
my name.
Udemy.
Lucas has joined the game, and this looks a lot cleaner than having to use two dots to concatenate
the string together or by using the String.format function.
It's very easy and it looks very clean and we can insert all types of values into these interpolated
strings.
For example, we can print out something like I can insert this value, and then I can put a number
in here like 1000 directly into my string.
I don't need to use the String.format function and I don't need to use any concatenation operators.
I'll print another string out and I'll say something like.
I can also insert any other property or expression as well.
And inside of here we can print out some random crap like in the workspace.
We could print one of its properties like the global wind, and then we can insert another thing by
putting another pair of curly brackets.
And then I'm going to put a backslash n here to move it to a new line.
And inside of here we could print something like, I don't know, maybe the game dot lighting.
And we get like the time of day.
So now if we go and playtest our game, we should have all this stuff print out in our console.
As you can see, I can insert this value 1000 directly into my string.
I can also insert any other property or expression as well.
There's our global wind and then there is our time of day.
And then once more, Udemy course has joined the game.
So as you can see, string interpolation is very powerful and very clean looking.
And guess what?
That's all for me for this lecture.
I hope you learned something new.
And I also hope you start using this awesome feature in your code.
See you in the next one!